package com.scopionstudio.newpaper;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.google.analytics.tracking.android.EasyTracker;
import com.scopionstudio.adapter.CategoryAdapter;
import com.scopionstudio.bean.RssParser;
import com.scopionstudio.bean.Variables;
import com.scopionstudio.network.CheckNetWork;
import com.scopionstudio.utils.RessItems;

public class CategoryActivity extends ScopionActivity implements OnItemClickListener {
	private int paper = 1;
	private int position;
	private ProgressDialog proGress;
	private ListView mList;
	private AsyncTask objMyTask = null;
	private long delayMillis = 30000; // 30s
	private ArrayList<RessItems> items = null;
	private boolean checkAnsytask;
	RssParser parser = new RssParser();
	private String TAG = "CategoryActivity";
	// private ImageButton btnBack;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listcategory);
		initVariable();
		/*
		 * Bundle bundle = getIntent().getExtras(); //get value of list paper =
		 * bundle.getInt(Variables.PAPER);
		 */
		// get value of PAPERS[] int position paper send by Intent
		setTitle(Variables.PAPERS[paper]);
		CategoryAdapter adapter = new CategoryAdapter(this,
				Variables.ICONS[paper], Variables.Categories[paper]);
		mList.setAdapter(adapter);
	}
	@Override
	protected void onStart() {
		super.onStart();
        EasyTracker.getInstance(getApplicationContext()).activityStart(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		// finish();
		super.onPause();
	}

	public void initVariable() {
		items = new ArrayList<RessItems>();
		mList = (ListView) findViewById(R.id.listCategory);
		mList.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
		// add new to fix error
		CheckNetWork objCheck = new CheckNetWork(getApplicationContext());
		if (objCheck.isOnline() == true) {
			try {
				objMyTask = new AsyncTask();
				objMyTask.execute(position);
			} catch (Exception e) {
				Log.e("Error Category", e.toString());
			}
		} else {
			MainMusicActivity objMain = new MainMusicActivity();
			objMain.showAlertDialog(CategoryActivity.this, getResources().getString(R.string.head_lost_connect),
					getResources().getString(R.string.ms_lost_connect), false);
		}
	}

	class AsyncTask extends android.os.AsyncTask<Integer, Integer, ArrayList<RessItems>> {
		@Override
		protected void onPreExecute() {
			proGress = new ProgressDialog(CategoryActivity.this);
			proGress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			proGress.setMessage(getResources().getString(R.string.ms_load_data));
			proGress.setCancelable(true);
			proGress.setCanceledOnTouchOutside(false);
			proGress.setOnCancelListener(new OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					dialog.dismiss();
					objMyTask.cancel(true);
				}
			});
			proGress.setIndeterminate(false);
			proGress.show();
		}

		@Override
		protected ArrayList<RessItems> doInBackground(Integer... params) {
			position = params[0];
				int key = paper * 1000 + params[0];
				items = parser.getList(Variables.Links[paper][position]);
				Log.e(TAG, "link" + Variables.Links[paper][position]);
				Variables.newMap.put(key, items);
			if(items != null){
				return items;
			}else{
				return null;
			}
		}

		// Update the progress
		@Override
		protected void onProgressUpdate(Integer... values) {
			// proGress.setProgress(values[0]);
		}

		@Override
		protected void onPostExecute( ArrayList<RessItems> result) {
			try {
				if (result != null ) {
					Intent intent = new Intent(CategoryActivity.this,
							ShowInfo.class);
					intent.putExtra(Variables.PAPER, paper);
					intent.putExtra(Variables.CATEGORY, position);
					intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
					startActivity(intent);
					checkAnsytask = true;
					proGress.dismiss();
					finish();
				} else {
					Toast.makeText(CategoryActivity.this,getResources().getString(R.string.ms_connect_slow), Toast.LENGTH_SHORT).show();
					proGress.dismiss();
					objMyTask.cancel(true);
				}

			} catch (Exception onPsot) {
				Log.e("onPsot Cate", onPsot.toString());
			}

		}
	}
	@Override
	public void onBackPressed() {
		AlertDialog.Builder mAlert = new AlertDialog.Builder(
				CategoryActivity.this);
		mAlert.setTitle(R.string.ct_logout);
		mAlert.setMessage(R.string.sms_logout)
				.setPositiveButton(R.string.user_yes,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								finish();
							}
						})
				.setNegativeButton(R.string.user_no,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
								dialog.dismiss();
							}
						});
		AlertDialog mAlertDialog = mAlert.create();
		mAlertDialog.show();
	}

    @Override
    protected void onStop() {
        super.onStop();
        EasyTracker.getInstance(getApplicationContext()).activityStop(this);
    }

}
